Package com.toc.logging

Source Code of com.toc.logging.Configuration

/*
* @(#) Configuration.java 1.0 24/12/2009
* Package: com.toc.logging
*
* Copyright 2009 TOC, Studio. All rights reserved.
*/
package com.toc.logging;

import java.io.OutputStream;
import java.util.HashMap;

import com.toc.lib.ExceptionHelper;
import com.toc.lib.GenericException;
import com.toc.logging.config.WriterConfig;

/**
* Title: Logging Configuration Object
* <p>Description: The configuration object of the logging, use {@link WriterConfig} to define the {@link com.toc.logging.writer.Writer Writer} of logging in a map and indicate the status of the logging.
* <p>Copyright: Copyright (c) 2009 Thousand Origami Cranes Corp.
* <p>Create Time: 25 Dec 2009 10:01:49
* @author Kevin.Zhou
* @version 1.0
* @see com.toc.logging.config.WriterConfig
* @see com.toc.logging.Log
*/
public class Configuration {
  /**
   * The map list of the writers definition.
   * The logging message will be write to each defined writers, if two writer with same domain,target and output been defined, the log also will be writer twice in same output.
   * <p><b>For example:</b>
   * <p>Add two writer of console as below
   * <p><code>addWriter(null, Log.CONSOLE, Log.DEBUG|Log.INFO,Log.DEFAULT_DATE_PATTERN...);<p>addWriter(null, Log.CONSOLE, Log.DEBUG,Log.DEFAULT_DATE_PATTERN...);</code>
   * <p>If you call <code>debug("Test")</code>, there will be only one message of "Test" at console.
   * <P>Bug if you call <code>info("Test")</code>. there will be two message of "Test" display at console.
   */
  private HashMap<String,WriterConfig> writers = null;
 
  /**
   * This is the flag of if the logging function is enable/disable.
   * <P>true - enabled
   * <p>false - disabled
   */
  private boolean enabled = true;
   
  /**
   * Constructs a {@link Configuration} object.
   */
  public Configuration() {
  }
 
  /**
   * Add a configuration of writer({@link WriterConfig}) by given parameter.
   * <p>This method is wrap the method {@code addWriter} will create a configuration of writer({@link WriterConfig}) with the default values of {@link WriterConfig#enabled enabled}(true) and {@link WriterConfig#wellFormat wellFormat}(false) and add it to map list of writers. For full coverage, please use the method {@link Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, boolean, boolean)}
   *
   * <p>Reference to {@link Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, boolean, boolean)}
   *
   * <p>Create Time: 25 Dec 2009 13:47:13
   * <br>Author: Kevin.Zhou
   */
  public void addWriter(String name, String domain, int target, int output, String pattern,
      String datePattern, String filePath, String logPath, String fileName,
      long maxFileSize, int separateFrequency) throws GenericException {
    try {
      addWriter(name, domain,target,output,pattern,datePattern,filePath,logPath,fileName,maxFileSize,separateFrequency,null, true, false);
    } catch(Exception e) {
      throw ExceptionHelper.parse(e);
    }   
  }
 
  /**
   * Add a configuration of writer({@link WriterConfig}) by given parameter.
   * <p>This method is wrap the method {@code addWriter} will create a configuration of writer({@link WriterConfig}) use {@code null} as default {@code stream} and add it to map list of writers.
   * <p><strong>Note:</strong> Because of stream writer is not supported yet, This method is recommend to be used because of provide full coverage of writer configuration option, and the method to add a configuration of stream writer is invisible to the user.
   *
   * <p>The description of parameter please reference to {@link Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, OutputStream, boolean, boolean)}
   *
   * <p>Create Time: 25 Dec 2009 14:11:31
   * <br>Author: Kevin.Zhou
   */
  public void addWriter(String name, String domain, int target, int output, String pattern,
      String datePattern, String filePath, String logPath, String fileName,
      long maxFileSize, int separateFrequency, boolean enabled, boolean wellFormat) throws GenericException {
    try {
      addWriter(name,domain,target,output,pattern,datePattern,filePath,logPath,fileName,maxFileSize,separateFrequency,null, enabled, wellFormat);
    } catch(Exception e) {
      throw ExceptionHelper.parse(e);
    }   
  }
 
  /**
   * Add a configuration of writer({@link WriterConfig}) by given parameter.
   * <p>This method is wrap the method {@code addWriter} will create a configuration of writer({@link WriterConfig}) and add it to map list of writers.
   * <p><strong>Note:</strong> Because of the stream writer is not supported yet, this method is only for internal use, you can use {@link Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, boolean, boolean)} to add a configuration of writer as recommend.
   *
   * <p>Reference to {@link Configuration#addWriter(String, String, int, int, String, String, String, String, String, long, int, boolean, boolean)}
   *
   * <p>Create Time: 25 Dec 2009 14:28:36
   * <br>Author: Kevin.Zhou
   * @param name The Name of the writer, it is the key store in writers map for program to easily get the writer from writers map.
   * @param domain The domain of the write, it is the class path which can decide the writer will write the message come from which class.
   * @param target The target where writer will write the log message. Values: {@link com.toc.logging.Log#CONSOLE CONSOLE},{@link com.toc.logging.Log#FILE FILE}. Can point to multiple target just like: {@code Log.CONSOLE|Log.FILE}
   * @param output Output will decide which type of message writer will write to target. Values: {@link com.toc.logging.Log#DEBUG DEBUG},{@link com.toc.logging.Log#INFO INFO},{@link com.toc.logging.Log#WARN WARN},{@link com.toc.logging.Log#ERROR ERROR}. Can point to multiple output just like: {@code Log.DEBUG|Log.INFO}
   * @param pattern The pattern of the message. The symbol refer to {@link com.toc.logging.Log#MESSAGE MESSAGE},{@link com.toc.logging.Log#DATE DATE},{@link com.toc.logging.Log#CLASS CLASS},{@link com.toc.logging.Log#LINE LINE},{@link com.toc.logging.Log#THREAD THREAD},{@link com.toc.logging.Log#TYPE TYPE}
   * @param datePattern Date pattern is define the date display style in log file, the symbol of the pattern refer to {@link java.text.SimpleDateFormat}
   * @param filePath File path is defined where to put the log files, it is a <strong>absolute path</strong>(full path) of the system.
   * @param logPath This parameter defined where to put the log history file if {@code maxFileSize} or {@code separateFrequency} is enabled. It is a <strong>absolute path</strong>(full path) of the system.
   * @param fileName The log file name, for the name of log history file is [File Last Modify Date(YYYYMMDD)][{@code fileName}].[Create Time(YYYYMMDDHHMISS)][n(n is a sequence #)], e.g.: log file name is 'Log.log' and history file will be '20091225Log.log.200912251500081'
   * @param maxFileSize The max size of the log file(Measurement in MB), any value < 1 will be seems as no limited. if the max size is defined the file will move to history folder if log file is full.
   * @param separateFrequency The frequency to separate the log file, once the interval is arrived, the log file will be move to History folder. Values: {@link com.toc.logging.Log#NEVER NEVER},{@link com.toc.logging.Log#DAILY DAILY},{@link com.toc.logging.Log#MONTHLY MONTHLY},{@link com.toc.logging.Log#YEARLY YEARLY}
   * @param stream The {@link OutputStream} object when the writer target contains {@link com.toc.logging.Log#STREAM STREAM} (Not support yet)
   * @param enabled If the writer is enabled, if disable the writer, no message will be written to the target. ({@code true} - enabled /{@code false} - disabled)
   * @param wellFormat The flag indicate if the well format function is open, please refer to {@link WriterConfig#wellFormat}
   * @throws GenericException this is the exception warp the all exception happened when add a configuration of writer. The error code please refer to {@link com.toc.lib.ExceptionCode ExceptionCode}
   */
  private void addWriter(String name, String domain, int target, int output, String pattern,
      String datePattern, String filePath, String logPath, String fileName,
      long maxFileSize, int separateFrequency, OutputStream stream, boolean enabled, boolean wellFormat) throws GenericException {
    try {
      // If the writers map is null, initial the writers map.
      if(writers == null) {
        writers = new HashMap<String,WriterConfig>();
      }
      writers.put(name, new WriterConfig(domain, target, output, pattern,
          datePattern, filePath, logPath, fileName,
          maxFileSize, separateFrequency, stream, enabled, wellFormat));
    } catch(Exception e) {
      throw ExceptionHelper.parse(e);
    }   
  }

  /**
   * Get the property value of {@link Configuration#enabled enabled}
   * <p>Create Time: 25 Dec 2009 15:58:57
   * <br>Author: Kevin.Zhou
   * @return {@link Configuration#enabled enabled}
   */
  public boolean isEnabled() {
    return enabled;
  }
 
  /**
   * Set the property value of {@link Configuration#enabled enabled}
   * @param enabled {@link Configuration#enabled enabled}
   * <p>Create Time: 25 Dec 2009 15:59:22
   * <br>Author: Kevin.Zhou
   */
  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
  }
 
  /**
   * Get the map of {@link Configuration#writers writers}
   * @return {@link Configuration#writers writers}
   * <p>Create Time: 25 Dec 2009 15:59:22
   * <br>Author: Kevin.Zhou
   */
  public HashMap<String,WriterConfig> getWriters() {
    return writers;
  }
 
  /**
   * Get the writer configuration({@link WriterConfig}) via given name from {@link Configuration#writers writers}
   * <p>Create Time: 25 Dec 2009 18:29:09
   * <br>Author: Kevin.Zhou
   * @param name The name of the writer configuration.
   * @return {@link WriterConfig}
   */
  public WriterConfig getWriter(String name) {
    return writers.get(name);
  }
}
TOP

Related Classes of com.toc.logging.Configuration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.